import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.EventListener;


@SuppressWarnings("serial")
public class DrawingCanvasView extends JPanel implements ModelListener 
{
	
	
	protected ObjectDrawModel model;
	protected DrawingCanvasController DCcontroller;
	protected Image imageBuffer;
	protected Graphics imageBufferGraphics;

	// constructor
	public DrawingCanvasView(ObjectDrawModel m) 
	{
		setBackground(Color.white);
		model = m;
		DCcontroller = createDrawingCanvasController();
		addDrawingCanvasListener(DCcontroller);
		model.addModelListener(this);
	}
  
	protected DrawingCanvasController createDrawingCanvasController() 
	{
		return new DrawingCanvasController(this, model);
    }

	protected void addDrawingCanvasListener(EventListener listener) 
	{
		addMouseListener((MouseListener) listener);
		addMouseMotionListener((MouseMotionListener) listener);
	}
  
	public void ModelUpdated() 
	{
		// null so far
	}

	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void paint(Graphics g) 
	{
		g.drawImage(imageBuffer,0, 0, this);
	}
  
	public void drawLineSegment(Point p1, Point p2) 
	{
		imageBufferGraphics.drawLine(p1.x, p1.y, p2.x, p2.y);
		repaint();
	}

	public void setBounds(int x, int y, int width, int height) 
	{
		Image newimageBuffer = createImage(width, height);
		imageBufferGraphics = newimageBuffer.getGraphics();
		if (imageBuffer != null) 
		{
			imageBufferGraphics.drawImage(imageBuffer, 0, 0 ,this);
		}
		imageBuffer = newimageBuffer;
		super.setBounds(x, y, width, height);
		repaint();
	}
	
	  public Graphics getimageBufferGraphics() {
		    return imageBufferGraphics;
		  }
	  
	// END OF CLASS
}